<?php 
class ControllerAccountInvoice extends Controller {
	function index() { 
		$config   =& $this->locator->get('config');
		$address  =& $this->locator->get('address');
		$currency =& $this->locator->get('currency');
		$customer =& $this->locator->get('customer');
		$database =& $this->locator->get('database');
		$language =& $this->locator->get('language');
		$module   =& $this->locator->get('module');
		$request  =& $this->locator->get('request');
		$response =& $this->locator->get('response');
		$session  =& $this->locator->get('session');
		$template =& $this->locator->get('template');
		$url      =& $this->locator->get('url');
		
    	if (!$customer->isLogged()) {
			if ($request->has('order_id')) {
				$session->set('redirect', $url->ssl('account_invoice', FALSE, array('order_id' => $request->gethtml('order_id'))));
			} else {
				$session->set('redirect', $url->ssl('account_invoice', FALSE, array('reference' => $request->gethtml('reference'))));
			}
      		
			$response->redirect($url->ssl('account_login'));
    	}
	
    	$language->load('controller/account_invoice.php');

    	$template->set('title', $language->get('heading_title'));
		
		if ($request->has('order_id')) {
			$order_info = $database->getRow("select * from `order` where order_id = '" . (int)$request->gethtml('order_id') . "' and customer_id = '" . (int)$customer->getId() . "'");
    	} else {
			$sql = "select * from `order` where reference = '?' and customer_id = '?'";
			$order_info = $database->getRow($database->parse($sql, $request->gethtml('reference'), $customer->getId()));
		}
		
		$view = $this->locator->create('template');

		$view->set('tpl', $template);
		
		$view->set('heading_title', $language->get('heading_title'));
		
		if ($order_info) {

    		$view->set('text_order', $language->get('text_order'));
			$view->set('text_email', $language->get('text_email'));
			$view->set('text_telephone', $language->get('text_telephone'));
			$view->set('text_fax', $language->get('text_fax'));
      		$view->set('text_shipping_address', $language->get('text_shipping_address'));
      		$view->set('text_shipping_method', $language->get('text_shipping_method'));
      		$view->set('text_payment_address', $language->get('text_payment_address'));
      		$view->set('text_payment_method', $language->get('text_payment_method'));
      		$view->set('text_order_history', $language->get('text_order_history'));
      		$view->set('text_product', $language->get('text_product'));
      		$view->set('text_model', $language->get('text_model'));
      		$view->set('text_quantity', $language->get('text_quantity'));
      		$view->set('text_price', $language->get('text_price'));
      		$view->set('text_total', $language->get('text_total'));

      		$view->set('column_date_added', $language->get('column_date_added'));
      		$view->set('column_status', $language->get('column_status'));
      		$view->set('column_comment', $language->get('column_comment'));
			
      		$view->set('button_continue', $language->get('button_continue'));

			$view->set('reference', $order_info['reference']);
			$view->set('email', $order_info['email']);
			$view->set('telephone', $order_info['telephone']);
			$view->set('fax', $order_info['fax']);
		
      		$shipping_address = array(
        		'firstname' => $order_info['shipping_firstname'],
        		'lastname'  => $order_info['shipping_lastname'],
        		'company'   => $order_info['shipping_company'],
        		'address_1' => $order_info['shipping_address_1'],
        		'address_2' => $order_info['shipping_address_2'],
        		'city'      => $order_info['shipping_city'],
        		'postcode'  => $order_info['shipping_postcode'],
        		'zone'      => $order_info['shipping_zone'],
        		'country'   => $order_info['shipping_country']
      		);

      		$view->set('shipping_address', $address->format($shipping_address, $order_info['shipping_address_format'], '<br />'));

			$view->set('shipping_method', $order_info['shipping_method']);

      		$payment_address = array(
        		'firstname' => $order_info['payment_firstname'],
        		'lastname'  => $order_info['payment_lastname'],
        		'company'   => $order_info['payment_company'],
        		'address_1' => $order_info['payment_address_1'],
        		'address_2' => $order_info['payment_address_2'],
        		'city'      => $order_info['payment_city'],
        		'postcode'  => $order_info['payment_postcode'],
        		'zone'      => $order_info['payment_zone'],
        		'country'   => $order_info['payment_country']
      		);

      		$view->set('payment_address', $address->format($payment_address, $order_info['payment_address_format'], '<br />'));

      		$view->set('payment_method', $order_info['payment_method']);

      		$products = $database->getRows("select * from order_product where order_id = '" . (int)$order_info['order_id'] . "'");

      		$product_data = array();

      		foreach ($products as $product) {
        		$options = $database->getRows("select * from order_option where order_id = '" . (int)$order_info['order_id'] . "' and order_product_id = '" . (int)$product['order_product_id'] . "'");

        		$option_data = array();

        		foreach ($options as $option) {
          			$option_data[] = array(
            			'name'  => $option['name'],
            			'value' => $option['value'],
          			);
        		}

				if ($config->get('config_tax')) {
					$product_data[] = array(
						'name'     => $product['name'],
						'model'    => $product['model'],
						'option'   => $option_data,
						'quantity' => $product['quantity'],
						'price'    => $currency->format(($product['price'] + ($product['price'] * $product['tax'] / 100)), $order_info['currency'], $order_info['value']),
						'discount' => (ceil($product['discount']) ? $currency->format(($product['price'] - $product['discount']) + (($product['price'] - $product['discount']) * $product['tax'] / 100), $order_info['currency'], $order_info['value']) : NULL),
						'total'    => $currency->format($product['total'] + ($product['total'] * $product['tax'] / 100), $order_info['currency'], $order_info['value'])
					);
				}
				else {
					$product_data[] = array(
						'name'     => $product['name'],
						'model'    => $product['model'],
						'option'   => $option_data,
						'quantity' => $product['quantity'],
						'price'    => $currency->format($product['price'], $order_info['currency'], $order_info['value']),
						'discount' => (ceil($product['discount']) ? $currency->format(($product['price'] - $product['discount']), $order_info['currency'], $order_info['value']) : NULL),
						'total'    => $currency->format($product['total'], $order_info['currency'], $order_info['value'])
					);
				}
      		}

      		$view->set('products', $product_data);

      		$view->set('totals', $database->getRows("select * from order_total where order_id = '" . (int)$order_info['order_id'] . "' order by sort_order"));

      		$history_data = array();

			$results = $database->getRows("select date_added, os.name as status, oh.comment from order_history oh left join order_status os on oh.order_status_id = os.order_status_id where oh.order_id = '" . (int)$order_info['order_id'] . "' and oh.notify = '1' order by oh.date_added");

      		foreach ($results as $result) {
        		$history_data[] = array(
          			'date_added' => $language->formatDate($language->get('date_format_short'), strtotime($result['date_added'])),
          			'status'     => $result['status'],
          			'comment'    => $result['comment']
        		);
      		}

      		$view->set('historys', $history_data);

      		$view->set('continue', $url->ssl('account_history'));

	  		$template->set('content', $view->fetch('content/account_invoice.tpl'));

      		$template->set($module->fetch());

      		$response->set($template->fetch('layout.tpl'));
    	} else {

      		$view->set('text_error', $language->get('text_error'));

      		$view->set('button_continue', $language->get('button_continue'));

      		$view->set('continue', $url->href('account_history'));
      
	  		$template->set('content', $view->fetch('content/error.tpl'));

      		$template->set($module->fetch());

      		$response->set($template->fetch('layout.tpl'));
    	}
  	}
}
?>
